package org.bingluo.livewallpaper.hunterxhunter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class MainLiveWallpaperService extends WallpaperService {
	private final Handler mHandler = new Handler();
	
	/**
	 * 启动一个WallpaperService.Engine 这个engine才是负责绘制壁纸以及响应与用户交互事件的核心部件
	 *  这个方法也是必须要重写的 并且是WallpaperService和service的主要区别 
	 */
	@Override
	public Engine onCreateEngine() {
		// TODO Auto-generated method stub
		System.out.println("onCreateEngine");
		return new MainEngine();
	}
	
	class MainEngine extends Engine
	{
		private final Paint mPaint = new Paint();
		private float centerX;
		private float centerY;
		private boolean mVisible;
		Bitmap bm1;
		Bitmap bm2;
		Bitmap bm3;
		Bitmap bm4;
		private int x =0;
		
		private final Runnable myDraw = new Runnable()
		{
			public void run()
			{
				draw();
			}
		};
		
		@Override
		public void onCreate(SurfaceHolder surfaceHolder) {
			System.out.println("onCreate");
			bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.d1);
			bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.d2);
			bm3 = BitmapFactory.decodeResource(getResources(), R.drawable.d3);
			bm4 = BitmapFactory.decodeResource(getResources(), R.drawable.d4);
			/**
			 * 让壁纸可以接受点击事件
			 */
			setTouchEventsEnabled(true);
			super.onCreate(surfaceHolder);
			
		}
		/**
		 * 这个一般在几个屏幕滑动的时候调用 
		 * 而且 当你拖动A屏幕向B屏幕过度时 
		 * 会随着B屏幕的显示面积的变大或者变小一直调用这个方法 
		 */
		@Override
		public void onOffsetsChanged(float xOffset, float yOffset,
				float xOffsetStep, float yOffsetStep, int xPixelOffset,
				int yPixelOffset) {
			// TODO Auto-generated method stub
			super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep,
					xPixelOffset, yPixelOffset);
		}
		
		/**
		 * 可以给壁纸发送一些命令 让其执行 
		 */
		@Override
		public Bundle onCommand(String action, int x, int y, int z,
				Bundle extras, boolean resultRequested) {
			// TODO Auto-generated method stub
			return super.onCommand(action, x, y, z, extras, resultRequested);
		}
		/**
		 * 点击屏幕事件，但是在oncreate里面一定在设置可以接受点击事件，setTouchEventsEnabled(true);
		 */
		@Override
		public void onTouchEvent(MotionEvent event) {
			if(event.getAction() == MotionEvent.ACTION_DOWN)
			{
				//手指按下的时候，记录它的坐标，并执行draw;
				centerX = event.getX();
				centerY = event.getY();
				draw();
			}
			else if(event.getAction() == MotionEvent.ACTION_UP)
			{
				// 手指离开的时候，重置画面
//				unDraw();;
			}
			else
			{
				//手指运动的时候记录坐标并执行draw
				centerX = event.getX();
				centerY = event.getY();
				draw();
			}
			super.onTouchEvent(event);
		}

		@Override
		public void onDestroy() {
			System.out.println("onDestroy");
			unDraw();
			super.onDestroy();
		}

		@Override
		public void onSurfaceChanged(SurfaceHolder holder, int format,
				int width, int height) {
			System.out.println("onSurfaceChanged#w:"+width+"#H:"+height+"#F:"+format);
			//保存屏幕中点坐标
			centerX = width/2.0f;
			centerY = height/2.0f;
			draw();
			super.onSurfaceChanged(holder, format, width, height);
		}
		/**
		 * 这个是屏幕显示或者不显示的时候调用的 必须完全覆盖屏幕才会调用 
		 */
		@Override
		public void onVisibilityChanged(boolean visible) {
			System.out.println("onVisibilityChanged#visiable:"+visible);
			mVisible = visible;
			if(visible)
			{
				//一般情况下，可见的时候去画图　
				draw();
			}
			else
			{
				//桌面不可见的时候停止画图
				mHandler.removeCallbacks(myDraw);
			}
			super.onVisibilityChanged(visible);
		}
		private void unDraw()
		{
			mHandler.removeCallbacks(myDraw);
		}
		private void draw()
		{
			final SurfaceHolder holder = getSurfaceHolder();
			Canvas c = holder.lockCanvas();
			c.drawColor(Color.WHITE);
			if(c!=null)
			{
				//依照x的值决定要画哪个bitmap
				if(x ==0)
				{
					c.drawBitmap(bm1, centerX-90, centerY-90, mPaint);
					x++;
				}
				else if(x ==1)
				{
					c.drawBitmap(bm2, centerX-90, centerY-90, mPaint);
					x++;
				}
				else if(x ==2)
				{
					c.drawBitmap(bm3, centerX-90, centerY-90, mPaint);
					x++;
				}
				else 
				{
					c.drawBitmap(bm4, centerX-90, centerY-90, mPaint);
					x = 0;
				}
				holder.unlockCanvasAndPost(c);
			}
			//停止draw
			unDraw();
			//设置一秒后再次执行
			if(mVisible)
			{
				mHandler.postDelayed(myDraw, 1000);
			}
		}
	}
}
